<?php

class Db_Tables_Menu extends Zend_Db_Table
{
    protected $_name = 'menu';
	var $user_group;
	var $shop_id;
	
	/**
	 * $_SESSION['backend_identity']['id_type']
	 * $_SESSION['backend_identity']['group_id']
	 */
	function MakeSystemMenu()
	{
		$result = array();
		$parents = array();
		$parent_child = array();
		$parent_child[0] = array();
		
		$select = $this->select();
		
		$select->where("menu_status = ?", 1);
		
		$select->order("sequence ASC");
		
		$data = $this->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				if($d['parent_id'])
				{
					if(!$parent_child[$d['parent_id']])
					{
						$parent_child[$d['parent_id']] = array();
					}
					
					$parent_child[$d['parent_id']][$d['menu_id']] = array(
						'menu_id' => $d['menu_id'],
						'menu_name' => $d['menu_name'],
						'url' => $d['url']
					);
					
				}else{
					$parents[] = $d['menu_id'];
					$parent_child[0][$d['menu_id']] = array(
						'menu_id' => $d['menu_id'],
						'menu_name' => $d['menu_name'],
						'url' => $d['url']
					);
				}
			}
			
			if(!empty($parents))
			{
				foreach($parents as $p)
				{
					$result[$p] = $parent_child[0][$p];
					
					if($parent_child[$p])
					{
						$result[$p]['submenu'] = array();
						
						foreach($parent_child[$p] as $pc)
						{
							$result[$p]['submenu'][] = $pc;
						}
					}
				}
			}
		}
		
		return $result;
	}
	
	function MenuDecolation()
	{
		$result = "";
		
		$menu = $this->MakeSystemMenu();
		
		if(!empty($menu))
		{
			foreach($menu as $mval)
			{
				$result .= '<li class="has-sub"><a  href="'.$mval['url'].'">'.$mval['menu_name'].'</a>';
				
				if($mval['submenu'])
				{
					$result .= '<ul>';
					
					foreach($mval['submenu'] as $sval)
					{
						$result .= '<li><a href="'.$sval['url'].'">'.$sval['menu_name'].'</a></li>';
					}
					
					$result .= '</ul>';
				}
					
				$result .= '</li>';
			}
		}
	
		return $result;
		
	}
}